//
//  XWalletHeadView.m
//  Bees
//
//  Created by Xiaohan on 2018/12/22.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XWalletHeadView.h"
#import "EOSAccountResult.h"
@implementation XWalletHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.backImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo( AdaptedWidth(8));
        make.bottom.mas_equalTo(-AdaptedWidth(8));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
    }];
}
- (void)setupUI{
    self.backImgV = [[UIImageView alloc]init];
    self.backImgV.image = IMAGE_NAMED(@"wallet_head_back");
    [self addSubview:self.backImgV];
   
    self.titleLab = [UILabel new];
    self.titleLab.font = kDefaultFont(12);
    self.titleLab.textColor = UIColor.blackColor;
    self.titleLab.text = XLocalized(@"我的资产");
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backImgV).offset(AdaptedWidth(20));
        make.top.equalTo(self.backImgV).offset(AdaptedWidth(23));
    }];
    self.seeBtn = [UIButton new];
    [self.seeBtn setImage:IMAGE_NAMED(@"open_eye") forState:UIControlStateNormal];
    [self.seeBtn setImage:IMAGE_NAMED(@"close_eye") forState:UIControlStateSelected];
    [self addSubview:self.seeBtn];
    [self.seeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLab.mas_centerY);
        make.left.equalTo(self.titleLab.mas_right).offset(AdaptedWidth(2));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(35), AdaptedWidth(35)));
    }];
    self.seeBtn.selected = IS_Property_Hidden;
    kWeakSelf(self);
    [self.seeBtn addTapBlock:^(UIButton * _Nonnull btn) {
        btn.selected = !btn.selected;
        if (btn.selected) {
            SET_Property_Hidden;
        }else{
            REMOVE_Property_Hidden;
        }
        if (IS_Property_Hidden) {
            [weakself setMoney:@"* * * *" unit:@""];
        }else{
            [weakself setMoney:[NSString stringWithFormat:@"%.*f",kSymbolLength_EOS,[weakself.max_valuation floatValue]] unit:weakself.unit];
        }
        !weakself.PropertyBlock?:weakself.PropertyBlock();
    }];
    
    self.assetsLab = [UILabel new];
    self.assetsLab.font = kBoldFont(24);
    self.assetsLab.textColor = UIColor.blackColor;
    [self addSubview:self.assetsLab];
    [self.assetsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLab);
        make.bottom.equalTo(self.backImgV).offset(-kNormalSpace);
    }];
}

- (void)setMoney:(NSString*)moeny unit:(NSString*)uint{
   
    NSString*unit = uint;
    NSString*money = moeny;
    NSMutableAttributedString *moneyAttr = [[NSMutableAttributedString alloc]initWithString:money.length==0?@"0.00":money];
    [moneyAttr setKern:@4];
    moneyAttr.font = kBoldFont(24);

    if (unit.length) {
        NSMutableAttributedString *unitAttr = [[NSMutableAttributedString alloc]initWithString:unit];
        unitAttr.font = kBoldFont(10);
        [moneyAttr appendAttributedString:unitAttr];
    }
    self.assetsLab.attributedText = moneyAttr;
}
- (void)setMax_valuation:(NSString *)max_valuation{
    _max_valuation = max_valuation;
    if (IS_Property_Hidden) {
        [self setMoney:@"* * * *" unit:@""];
    }else{
        [self setMoney:handlerDecimalNumber(max_valuation,NSRoundPlain, kSymbolLength_EOS).stringValue unit:self.unit];
    }
}
- (NSString*)unit{
    if (!_unit.length) {
        return @"CNY";
    }
    return _unit;
}
@end
